package com.dao.impl;

import com.dao.Vip;
import com.util.dbCRUD;

import java.sql.ResultSet;
import java.sql.SQLException;

import static com.util.Vessel.vipCRUD;


public class VipImpl implements Vip {

    //vip判断
    @Override
    public boolean vipJudge(int cId) {
        boolean reValue = false;
        String sql = "SELECT cVip\n" +
                "FROM clients\n" +
                "WHERE cId = ?";
        Object[] object = new Object[1];
        object[0] = cId;
        ResultSet resultSet = vipCRUD.printAll(sql,object);
        try{
            while(resultSet.next()){
                String vip = resultSet.getString("cVip");
                if (vip != null){
                    reValue = true;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return reValue;
    }

    //余额查询
    @Override
    public double vipBalance(int cId,String vipPassword) {
        String sql = "SELECT cBalance\n" +
                "FROM clients\n" +
                "WHERE cId = ?\n" +
                "    AND vipPassword = ?";
        Object[] object = new Object[2];
        object[0] = cId;
        object[1] = vipPassword;
        ResultSet resultSet = vipCRUD.printAll(sql,object);
        //double[] balances = new double[1];
        double balance = 0;
        try{
            while (resultSet.next()){
                balance = resultSet.getDouble("cBalance");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return balance;
    }

    //vip余额查询，无序密码版本
    public double Balance(int cId) {
        String sql = "SELECT cBalance\n" +
                "FROM clients\n" +
                "WHERE cId = ?\n";
        Object[] object = new Object[1];
        object[0] = cId;
        ResultSet resultSet = vipCRUD.printAll(sql,object);
        //double[] balances = new double[1];
        double balance = 0;
        try{
            while (resultSet.next()){
                balance = resultSet.getDouble("cBalance");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return balance;
    }

    //充钱
    @Override
    public double chargeMoney(int cId, String vipPassword,double money) {
        String sql = "UPDATE clients\n" +
                "SET cBalance = cBalance + ?\n" +
                "WHERE cId = ?\n" +
                "    AND vipPassword = ?;";
        Object[] objects = new Object[3];
        objects[0] = money;
        objects[1] = cId;
        objects[2] = vipPassword;
        vipCRUD.update(sql,objects);
        double balance = vipBalance(cId,vipPassword);
        return balance;
    }

    //开通会员
    @Override
    public boolean joinVip(int cId,String vipPassword) {
        String sql = "UPDATE clients\n" +
                "SET cVip = 'true' ,vipPassword = ?\n" +
                "WHERE cId = ?;";
        Object[] objects = new Object[2];
        objects[0] = vipPassword;
        objects[1] = cId;
        int num = vipCRUD.update(sql,objects);
        boolean isFlag = (num > 0);
        return isFlag;
    }

}
